package com.example.spareparts.controller;

import com.example.spareparts.entity.User;
import com.example.spareparts.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/listall")
    public List<User> findAllUser()
    {
        return userService.findAllUser();
    }
    @PostMapping("/insert")
    public void insertUser(@RequestBody User user){
        userService.insertUser(user);
    }
    @PutMapping("/update/{userid}")
    public void updateUser(@PathVariable Long userid,@RequestBody User user){
        user.setUserid(userid);
        userService.updateUser(user);
    }
    @DeleteMapping("/delete/{userid}")
    public void deleteUser(@PathVariable Long userid){
        userService.deleteUser(userid);
    }
    @GetMapping("/findbyid/{userid}")
    public User findUserById(@PathVariable Long userid){
        return userService.findUserById(userid);
    }
    @GetMapping("/findbyusername/{username}")
    public User findUserByUsername(@PathVariable String username){
        return userService.findUserByUsername(username);
    }
}
